package com.zhuoyue.web.controller.system;

import com.zhuoyue.common.core.domain.*;
import com.zhuoyue.common.utils.poi.ExcelUtil;
import com.zhuoyue.system.domain.po.SelectTimePo;
import com.zhuoyue.system.service.ISysStatisticService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author zhanghui
 */
@RestController
@RequestMapping("/system/statistic")
public class SysStatisticController {
    @Autowired
    ISysStatisticService sysStatisticService;

    /**
     * 单个用户设备数量统计
     *
     * @param orgCode
     * @return
     */
    @GetMapping("/userDevice/list")
    public AjaxResult getUserDeviceCount(String account, String orgCode) {
        List<ChartData> userDeviceChart = sysStatisticService.getUserDeviceChart(account, orgCode);
        // 排序
        userDeviceChart.sort(new Comparator<ChartData>() {
            @Override
            public int compare(ChartData o1, ChartData o2) {
                return o1.getCount() - o2.getCount();
            }
        });
        return AjaxResult.success(userDeviceChart);
    }

    /**
     * 导出单个用户设备数量统计
     *
     * @param orgCode
     * @return
     */
    @GetMapping("/userDevice/export")
    public AjaxResult exportUserDeviceCount(String account, String orgCode) {
        List<ChartData> userDeviceChart = sysStatisticService.getUserDeviceChart(account, orgCode);
        // 排序
        userDeviceChart.sort(new Comparator<ChartData>() {
            @Override
            public int compare(ChartData o1, ChartData o2) {
                return o1.getCount() - o2.getCount();
            }
        });
        Collections.reverse(userDeviceChart);

        List<DeviceCountChartExport> exportList = new ArrayList<>();
        userDeviceChart.forEach(item -> {
            DeviceCountChartExport chartExport = new DeviceCountChartExport();
            BeanUtils.copyProperties(item, chartExport);
            exportList.add(chartExport);
        });
        ExcelUtil<DeviceCountChartExport> util = new ExcelUtil<>(DeviceCountChartExport.class);
        return util.exportExcel(exportList, account + "设备统计");
    }

    /**
     * 产品数量统计
     *
     * @param orgCode
     * @return
     */
    @GetMapping("/productCount/list")
    public AjaxResult getProductCount(String orgCode) {
        return AjaxResult.success(sysStatisticService.getProductCountChart(orgCode));
    }

    /**
     * 产品数量统计导出
     *
     * @param orgCode
     * @return
     */
    @GetMapping("/productCount/export")
    public AjaxResult productCountExport(String orgCode) {
        List<ChartData> productCountChart = sysStatisticService.getProductCountChart(orgCode);
        List<ProductCount> productCountList = new ArrayList<>();
        for (ChartData chartData : productCountChart) {
            ProductCount productCount = new ProductCount();
            productCount.setProductName(chartData.getAxisData());
            productCount.setCount(chartData.getCount());
            productCountList.add(productCount);
        }

        Collections.reverse(productCountList);

        ExcelUtil<ProductCount> excelUtil = new ExcelUtil<>(ProductCount.class);
        return excelUtil.exportExcel(productCountList, "设备统计");
    }

    /**
     * 查询用户新增统计
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/userRegister/list")
    public AjaxResult userRegisterStatistic(Long startTime, Long endTime, Long deptId) {
        SelectTimePo selectTimePo = new SelectTimePo();
        selectTimePo.setSelectStartTime(new Date(startTime));
        selectTimePo.setSelectEndTime(new Date(endTime));
        selectTimePo.setDeptId(deptId);
        return AjaxResult.success(sysStatisticService.getUserRegisterRangeTimeChart(selectTimePo));
    }

    /**
     * 导出用户新增统计
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/userRegister/export")
    public AjaxResult exportUserRegisterStatistic(Long startTime, Long endTime, Long deptId) {
        SelectTimePo selectTimePo = new SelectTimePo();
        selectTimePo.setSelectStartTime(new Date(startTime));
        selectTimePo.setSelectEndTime(new Date(endTime));
        selectTimePo.setDeptId(deptId);
        List<ChartData> userRegisterRangeTimeChart = sysStatisticService.getUserRegisterRangeTimeChart(selectTimePo);
        List<TimeChartExport> exportList = new ArrayList<>();
        userRegisterRangeTimeChart.forEach(item -> {
            TimeChartExport chartExport = new TimeChartExport();
            BeanUtils.copyProperties(item, chartExport);
            exportList.add(chartExport);
        });
        ExcelUtil<TimeChartExport> util = new ExcelUtil<>(TimeChartExport.class);
        return util.exportExcel(exportList, "用户图表");
    }

    /**
     * 查询设备新增统计
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/deviceIncrease/list")
    public AjaxResult deviceIncreaseStatistic(Long startTime, Long endTime, Long deptId) {
        SelectTimePo selectTimePo = new SelectTimePo();
        selectTimePo.setSelectStartTime(new Date(startTime));
        selectTimePo.setSelectEndTime(new Date(endTime));
        selectTimePo.setDeptId(deptId);
        return AjaxResult.success(sysStatisticService.getDeviceIncreaseRangeTimeChart(selectTimePo));
    }

    /**
     * 导出设备新增统计
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/deviceIncrease/export")
    public AjaxResult exportDeviceIncreaseStatistic(Long startTime, Long endTime, Long deptId) {
        SelectTimePo selectTimePo = new SelectTimePo();
        selectTimePo.setSelectStartTime(new Date(startTime));
        selectTimePo.setSelectEndTime(new Date(endTime));
        selectTimePo.setDeptId(deptId);
        List<ChartData> deviceIncreaseRangeTimeChart = sysStatisticService.getDeviceIncreaseRangeTimeChart(selectTimePo);
        List<TimeChartExport> exportList = new ArrayList<>();
        deviceIncreaseRangeTimeChart.forEach(item -> {
            TimeChartExport chartExport = new TimeChartExport();
            BeanUtils.copyProperties(item, chartExport);
            exportList.add(chartExport);
        });
        ExcelUtil<TimeChartExport> util = new ExcelUtil<>(TimeChartExport.class);
        return util.exportExcel(exportList, "设备图表");
    }

    /**
     * 查询网关新增统计
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/gatewayIncrease/list")
    public AjaxResult gatewayIncreaseStatistic(Long startTime, Long endTime, Long deptId) {
        SelectTimePo selectTimePo = new SelectTimePo();
        selectTimePo.setSelectStartTime(new Date(startTime));
        selectTimePo.setSelectEndTime(new Date(endTime));
        selectTimePo.setDeptId(deptId);
        return AjaxResult.success(sysStatisticService.getGatewayIncreaseRangeTimeChart(selectTimePo));
    }

    /**
     * 导出网关新增统计
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/gatewayIncrease/export")
    public AjaxResult exportGatewayIncreaseStatistic(Long startTime, Long endTime, Long deptId) {
        SelectTimePo selectTimePo = new SelectTimePo();
        selectTimePo.setSelectStartTime(new Date(startTime));
        selectTimePo.setSelectEndTime(new Date(endTime));
        selectTimePo.setDeptId(deptId);
        List<ChartData> gatewayIncreaseRangeTimeChart = sysStatisticService.getGatewayIncreaseRangeTimeChart(selectTimePo);
        List<TimeChartExport> exportList = new ArrayList<>();
        gatewayIncreaseRangeTimeChart.forEach(item -> {
            TimeChartExport chartExport = new TimeChartExport();
            BeanUtils.copyProperties(item, chartExport);
            exportList.add(chartExport);
        });
        ExcelUtil<TimeChartExport> util = new ExcelUtil<>(TimeChartExport.class);
        return util.exportExcel(exportList, "网关图表");
    }

    /**
     * 账号消息数统计
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/accountMessageCount/list")
    public AjaxResult accountMessageCountStatistic(Long startTime, Long endTime, Long deptId) {
        SelectTimePo selectTimePo = new SelectTimePo();
        selectTimePo.setSelectStartTime(new Date(startTime));
        selectTimePo.setSelectEndTime(new Date(endTime));
        selectTimePo.setDeptId(deptId);
        return AjaxResult.success(sysStatisticService.getAccountMessageCountRangeTimeChart(selectTimePo));
    }

    /**
     * 导出账号消息数统计
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/accountMessageCount/export")
    public AjaxResult exportAccountMessageCountStatistic(Long startTime, Long endTime, Long deptId) {
        SelectTimePo selectTimePo = new SelectTimePo();
        selectTimePo.setSelectStartTime(new Date(startTime));
        selectTimePo.setSelectEndTime(new Date(endTime));
        selectTimePo.setDeptId(deptId);
        List<ChartData> accountMessageCountRangeTimeChart = sysStatisticService.getAccountMessageCountRangeTimeChart(selectTimePo);

        List<TimeChartExport> exportList = new ArrayList<>();
        accountMessageCountRangeTimeChart.forEach(item -> {
            TimeChartExport chartExport = new TimeChartExport();
            BeanUtils.copyProperties(item, chartExport);
            exportList.add(chartExport);
        });
        ExcelUtil<TimeChartExport> util = new ExcelUtil<>(TimeChartExport.class);
        return util.exportExcel(exportList, "账号消息");
    }

    /**
     * 网关消息数统计
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/gatewayMessageCount/list")
    public AjaxResult gatewayMessageCountStatistic(Long startTime, Long endTime, Long deptId) {
        SelectTimePo selectTimePo = new SelectTimePo();
        selectTimePo.setSelectStartTime(new Date(startTime));
        selectTimePo.setSelectEndTime(new Date(endTime));
        selectTimePo.setDeptId(deptId);
        return AjaxResult.success(sysStatisticService.getGatewayMessageCountRangeTimeChart(selectTimePo));
    }

    /**
     * 导出网关消息数统计
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/gatewayMessageCount/export")
    public AjaxResult exportGatewayMessageCountStatistic(Long startTime, Long endTime, Long deptId) {
        SelectTimePo selectTimePo = new SelectTimePo();
        selectTimePo.setSelectStartTime(new Date(startTime));
        selectTimePo.setSelectEndTime(new Date(endTime));
        selectTimePo.setDeptId(deptId);
        List<ChartData> gatewayMessageCountRangeTimeChart = sysStatisticService.getGatewayMessageCountRangeTimeChart(selectTimePo);

        List<TimeChartExport> exportList = new ArrayList<>();
        gatewayMessageCountRangeTimeChart.forEach(item -> {
            TimeChartExport chartExport = new TimeChartExport();
            BeanUtils.copyProperties(item, chartExport);
            exportList.add(chartExport);
        });
        ExcelUtil<TimeChartExport> util = new ExcelUtil<>(TimeChartExport.class);
        return util.exportExcel(exportList, "网关消息");
    }
}
